package com.tmc.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.baseinfo.Airline;
import com.tmc.service.baseinfo.AirlineServiceImpl;
import com.tmc.web.BaseController;

@Controller
@RequestMapping("/airline")
public class AirlineController extends BaseController {

	@Autowired
	private AirlineServiceImpl service;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView findist(Airline cond, Integer pageIndex, Integer pageSize, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<Airline> page = service.findByCon(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("cond", cond);
		map.put("context", context);
		return new ModelAndView("views/baseInfo/airline-list", map);
	}
	
	@RequestMapping(value = "/get", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getByCode(String code) {
		Airline bean = service.getByCode(code);
		return JSON.toJSONString(bean);
	}
	
	@RequestMapping(value = "/all", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String findAll() {
		List<Airline> all = service.findAllFromCache();
		return JSON.toJSONString(all);
	}
	
	@RequestMapping(value = "/editPage")
	public ModelAndView editPage(Long airlineId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		Airline airline = null;
		if (airlineId != null)
			airline = service.getById(context, airlineId);
		else
			airline = new Airline();
		map.put("airline", airline);
		map.put("context", context);
		return new ModelAndView("views/baseinfo/airline-edit", map);
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(Airline airline) {
		CommonResult cr = service.save(new ContextInfo(), airline);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(Long airlineId) throws Exception {
		CommonResult cr = service.delete(new ContextInfo(), airlineId);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/updatefromfile", method = RequestMethod.POST)
	public ModelAndView updatefromfile(HttpServletRequest request) throws Exception {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		service.updateFromFile(context);
		map.put("cond", new Airline());
		map.put("page", new Pagination<Airline>());
		map.put("context", context);
		return new ModelAndView("views/baseInfo/airline-list", map);
	}
	
	@RequestMapping(value="searchAirline")
	@ResponseBody
	public String searchAirline(String query){
		List<Airline> list = service.searchAirline(query);
		JSONArray array = new JSONArray();
		//"Key":"airlineCode","Name":"airlineName(airlineCode)" 注意，键和值都是字符串首字母大写
		for (Airline airline : list) {
			JSONObject obj = new JSONObject();
			obj.put("Key", airline.getAirlineCode());
			obj.put("Name", airline.getAirlineName()+"("+airline.getAirlineCode()+")");
			array.add(obj);
		}	
		return array.toJSONString();	
	}
	
}
